//
//  ViewController.swift
//  SwiftExampleProject
//
//  Created by 蓓蕾 on 2024/2/18.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let arr = [1, 2, 3]
        print(arr)
//        consume arr
//        print(arr)
        createUI()
        
        //MARK: - 字符串的截取
        let testString = "生命诚可贵，爱情价更高"
        
        let startIndex = testString.index(testString.startIndex, offsetBy: 3)
        let endIndex = testString.index(testString.endIndex, offsetBy: -3)
        /// 方法一
        let toResultString = testString[testString.startIndex..<startIndex]
        print(toResultString)
        /// 方法二
        let fromResultString = testString[startIndex...]
        print(fromResultString)
        /// 方法三
        let resultString = testString[startIndex..<endIndex]
        print(resultString)
        
        if testString.contains("爱情") {
            print("包含字符串\"爱情\"")
        } else {
            print("不包含字符串\"爱情\"")
        }
        
        print("【" + resultString  + "】的字符串个数为：" + "\(resultString.count)")
        
    }
    
    func createUI() {
        let titleView = UILabel()
        self.view.addSubview(titleView)
//        titleView.frame = CGRect(x: 100, y: 300, width: 300, height: 60)
        titleView.textColor = UIColor.orange
        titleView.font = UIFont.systemFont(ofSize: 20)
        titleView.numberOfLines = 0
        let titleString = "生命诚可贵，爱情价更高。\n若为自由故，两者皆可抛。";
        titleView.text = titleString
        titleView.textAlignment = NSTextAlignment.center
        let attributedString = NSMutableAttributedString(string: titleString)
        attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 80), range: NSMakeRange(2, 2))
        attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSMakeRange(2, 2))
        attributedString.addAttributes([NSAttributedString.Key.strokeColor : UIColor.black, NSAttributedString.Key.strokeWidth : 2], range: NSMakeRange(2, 2))
        attributedString.addAttributes([NSAttributedString.Key.strokeColor : UIColor.red, NSAttributedString.Key.strokeWidth : 2, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 40)], range: NSMakeRange(6, 2))
        titleView.attributedText = attributedString
        titleView.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
        }
        //MARK: - 字符串切割
        let resultArr = titleString.components(separatedBy: "，")
        print(resultArr.count)
        print(resultArr)
        
        for i in resultArr {
            print(i)
            print("")
        }
        print("方法一")
        print(titleString.charactersArray)
        print("")
        print("方法二")
        print(titleString.charactersArrayMethod())
        print("")
        print("方法三")
        print(titleString.charactersArrayMethodTwo(parama: titleString))
        print("")
        print("方法四")
        print(charactersArray(parama: titleString))
        print("")
        print("方法五")
        print(Array(titleString))
    }
    /// 将一个字符串切割成单个字符数组
    func charactersArray(parama:String) -> [Character] {
        return Array(parama)
    }

}

extension String {
    func charactersArrayMethod() -> [Character] {
        return Array(self)
    }
    
    func charactersArrayMethodTwo(parama:String) -> [Character] {
        return Array(parama)
    }
    
    public var charactersArray : [Character] {
        return Array(self)
    }
}

//extension String {
//
//    func index(from: Int) -> Index {
//        return self.index(startIndex, offsetBy: from)
//    }
//
//    func substring(from: Int) -> String {
//        let fromIndex = index(from: from)
//        return String(self[fromIndex...])
//    }
//
//    func substring(to: Int) -> String {
//        let toIndex = index(from: to)
//        return String(self[..<toIndex])
//    }
//
//    func substring(with r: Range<Int>) -> String {
//        let startIndex = index(from: r.lowerBound)
//        let endIndex = index(from: r.upperBound)
//        return String(self[startIndex..<endIndex])
//    }
//}


